#include <bits/stdc++.h>
#include <cstdint>
using namespace std;

class Solution {
private:
    bool isNum(char chr){
        return (chr <= '9' && chr >= '0');
    }
public:
    int myAtoi(string s) {
        int flag = 0;
        int below = 0;
        long long res = 0;
        for(auto& chr: s){
            if(isNum(chr)){
                res *= 10;
                res += chr-'0';
                flag = 1;
                if(res > INT32_MAX) break;
            }
            else if(chr == '-' && flag == 0){
                flag = 1;
                below = 1;
            }
            else if(chr == '+' && flag == 0){
                below = 0;
                flag = 1;
            }
            else if(chr==' ' && flag == 0) continue;
            else break;
        }
        if(below) res = -res;
        if(res < INT32_MIN) res = INT32_MIN;
        else if(res > INT32_MAX) res = INT32_MAX;
        return res;
    }
};

int main(){
    string s = " -1337c0d3";
    Solution solution;
    cout<<solution.myAtoi(s);
    return 0;
}
//test
